<template>
  <el-dialog :append-to-body="true" :visible.sync="dialog" title="批量新增" width="940px">
    <el-form ref="form" :model="form" :inline="true" :rules="rules" size="small" label-width="125px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="发起时间">
            <el-date-picker
              v-model="form.startTime"
              type="datetime"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择日期时间"
              style="width: 300px"
              :disabled="isView"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客户名称" prop="customerName">
            <el-select v-model="form.customerName" default-first-option placeholder="请选择客户" :disabled="isView" class="input_text" @change="handleSelect($event)">
              <el-option v-for="(item,index) in customerItem" :key="item.id + index" :label="item.customerName" :value="item" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="任务名称" prop="taskTitle">
            <el-input v-model="form.taskTitle" class="row_length" :readonly="isView" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="任务内容" prop="taskContent">
            <el-input v-model="form.taskContent" class="row_length" :rows="4" type="textarea" :readonly="isView" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="附件" prop="fileList">
            <el-upload
              ref="upload"
              :limit="1"
              :file-list="form.fileList"
              :on-change="beforeUpload"
              :on-remove="handleBeforeRemove"
              :auto-upload="false"
              :headers="headers"
              :data.sync="form"
              :on-success="handleSuccess"
              :on-error="handleError"
              :action="baseApi + '/api/tTasks/batch'"
            >
              <div class="eladmin-upload"><i class="el-icon-upload" /> 选择附件</div>
            </el-upload>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { mapGetters } from 'vuex'
import { getToken } from '@/utils/auth'
import { parseTime } from '@/utils/index'
import request from '@/utils/request'

export default {
  props: {
    isView: {
      type: Boolean,
      required: true
    },
    supThis: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      customerItem: [],
      headers: {
        'Authorization': 'Bearer ' + getToken()
      },
      dialogImageUrl: '',
      timeDefaultShow: new Date(),
      dialogVisible: false,
      loading: false,
      dialog: false,
      form: {
        taskTitle: '',
        taskContent: '',
        taskAddr: '',
        customerName: '',
        customerId: '',
        startTime: parseTime(new Date().getTime()),
        fileList: []
      },
      rules: {
        customerName: [
          { required: true, message: '请输入客户名称', trigger: 'blur' }
        ],
        taskTitle: [
          { required: true, message: '请输入任务名称', trigger: 'blur' }
        ],
        taskContent: [
          { required: true, message: '请输入任务内容', trigger: 'blur' }
        ],
        startTime: [
          { required: true, message: '请输入发起时间', trigger: 'blur' }
        ],
        fileList: [
          { required: true, message: '请选择文件', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters([
      'baseApi'
    ])
  },
  mounted() {
    this.tcustmerInfoAll()
  },
  methods: {
    tcustmerInfoAll() {
      request({
        url: 'api/tCustomerInfoAll',
        method: 'get'
      }).then(res => {
        this.customerItem = res
      })
    },
    handleSelect(item) {
      this.form.customerName = item.customerName
      this.form.customerId = item.id
    },
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      console.log(this.$refs['form'])
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          this.$refs.upload.submit()
        } else {
          return false
        }
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = {
        id: '',
        taskTitle: '',
        taskContent: '',
        notes: '',
        linker: '',
        cellphone: '',
        inputOperator: '',
        startTime: parseTime(new Date().getTime()),
        customerName: '',
        customerId: '',
        fileList: []
      }
    },
    beforeUpload(file) {
      console.log(file)
      this.form.fileList.push(file)
    },
    handleSuccess(response, file, fileList) {
      this.loading = false
      this.$notify({
        title: '增加成功',
        type: 'success',
        duration: 2500
      })
      this.resetForm()
      this.$parent.$parent.init()
      this.dialog = false
    },
    handleBeforeRemove(file, fileList) {
      this.form.fileList.splice(0, 1)
      return true
    },
    handleError(e, file, fileList) {
      const msg = JSON.parse(e.message)
      this.$notify({
        title: msg.message,
        type: 'error',
        duration: 2500
      })
      this.loading = false
    }
  }
}
</script>

<style scoped>
.row_length {
    width: 760px
}
.input_text {
    width: 300px
}
</style>
